﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace Travel.Web.route
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtname.Text.Trim().Length == 0)
            {
                strErr += "线路名称不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txttype.Value))
            {
                strErr += "1 国内 2 国外 等  按照格式错误！\\n";
            }
            if (this.txtkey_words.Text.Trim().Length == 0)
            {
                strErr += "关键字 用于搜索不能为空！\\n";
            }
            if (this.txtstart_with.Text.Trim().Length == 0)
            {
                strErr += "出发地不能为空！\\n";
            }
            if (this.txtend_with.Text.Trim().Length == 0)
            {
                strErr += "目的地不能为空！\\n";
            }

            if (!PageValidate.IsNumber(txtdays_account.Text))
            {
                strErr += "天数格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtbegin_date.Text))
            {
                strErr += "出发日期格式错误！\\n";
            }

            #region 线路说明
            // if (!PageValidate.IsNumber(.Text))
            //{
            //    strErr += "线路Id格式错误！\\n";
            //}
            if (this.editor_txtfeature.Text.Trim().Length == 0)
            {
                strErr += "行程特色不能为空！\\n";
            }
            if (this.editor_txtroute_contains.Text.Trim().Length == 0)
            {
                strErr += "行程包含不能为空！\\n";
            }
            if (this.editor_txtroute_not_contains.Text.Trim().Length == 0)
            {
                strErr += "行程不包含不能为空！\\n";
            }
            if (this.editor_txtown_expense.Text.Trim().Length == 0)
            {
                strErr += "自费项目不能为空！\\n";
            }
            if (this.editor_txtshopping_area.Text.Trim().Length == 0)
            {
                strErr += "购物场所不能为空！\\n";
            }
            if (this.editor_txtservice.Text.Trim().Length == 0)
            {
                strErr += "服务标准不能为空！\\n";
            }
            if (this.editor_txtfriend.Text.Trim().Length == 0)
            {
                strErr += "友情提醒不能为空！\\n";
            }
            if (this.editor_notice.Text.Trim().Length == 0)
            {
                strErr += "签证须知不能为空！\\n";
            }
            if (this.edtor_content.Text.Trim().Length == 0)
            {
                strErr += "线路说明不能为空！\\n";
            }
            #endregion

            int startId = 0;
            int endId = 0;

            BLL.place place = new BLL.place();
            var placeModel = place.GetModelList(string.Format("name = '{0}'", txtstart_with.Text));
            if (placeModel == null || placeModel.Count == 0)
            {
                var satrtModel = new Model.place()
                    {
                        name = txtstart_with.Text,
                        created_on = DateTime.Now,
                        created_by = User.Identity.Name
                    };
                startId = place.Add(satrtModel);
            }
            else
            {
                startId = placeModel[0].id;
            }
            placeModel = place.GetModelList(string.Format("name = '{0}'", txtend_with.Text));
            if (placeModel == null || placeModel.Count == 0)
            {
                var satrtModel = new Model.place()
                {
                    name = txtstart_with.Text,
                    created_on = DateTime.Now,
                    created_by = User.Identity.Name
                };
                endId = place.Add(satrtModel);
            }
            else
            {
                endId = placeModel[0].id;
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string name = this.txtname.Text;
            int type = int.Parse(this.txttype.Value);
            string key_words = this.txtkey_words.Text;
            string start_with = this.txtstart_with.Text;
            string end_with = this.txtend_with.Text;
            int start_id = startId;
            int end_id = endId;
            int days_account = int.Parse(this.txtdays_account.Text);
            DateTime begin_date = DateTime.Parse(this.txtbegin_date.Text);
            DateTime created_on = DateTime.Now;
            string created_by = User.Identity.Name;//创建人

            Travel.Model.route model = new Travel.Model.route();

            model.name = name;
            model.type = type;
            model.key_words = key_words;
            model.start_with = start_with;
            model.end_with = end_with;
            model.start_id = start_id;
            model.end_id = end_id;
            model.days_account = days_account;
            model.begin_date = begin_date;
            model.created_on = created_on;
            model.created_by = User.Identity.Name;

            Travel.BLL.route bll = new Travel.BLL.route();
            int route_id = bll.Add(model);

            #region 线路说明
            string feature = this.editor_txtfeature.Text;
            string route_contains = this.editor_txtroute_contains.Text;
            string rout_not_contains = this.editor_txtroute_not_contains.Text;
            string own_expense = this.editor_txtown_expense.Text;
            string shopping_area = this.editor_txtshopping_area.Text;
            string service = this.editor_txtservice.Text;
            string friend_ship_tip = this.editor_txtfriend.Text;
            string visa_notice = this.editor_notice.Text;
            string r_content = this.edtor_content.Text;

            Travel.Model.route_content contentModel = new Travel.Model.route_content();
            contentModel.route_id = route_id;
            contentModel.feature = feature;
            contentModel.route_contains = route_contains;
            contentModel.rout_not_contains = rout_not_contains;
            contentModel.own_expense = own_expense;
            contentModel.shopping_area = shopping_area;
            contentModel.service = service;
            contentModel.friend_ship_tip = friend_ship_tip;
            contentModel.visa_notice = visa_notice;
            contentModel.r_content = r_content;
            contentModel.created_on = created_on;
            contentModel.created_by = created_by;

            Travel.BLL.route_content contentBll = new Travel.BLL.route_content();
            contentBll.Add(contentModel);
            #endregion

            Response.Redirect("~/admin/route/modify.aspx?id=" + route_id);

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
